module.exports = {
	// app端因为地图组件需要用到nvue页面
	// 但是nvue无法直接取main文件配置的全局参数 所以使用APP.vue进行中转
	// 组件created中 或者 或者页面 onload中添加该方法进行初始化
	// 初始化COMMON_VAR change_var 以及设置重置页面数据，或者执行初始化回调

	async init_pagedata() {
		let _this = this;
		let _config = this.com_set_config();
		_this.change_var = this.deepclone(_config.main_change_var);
		// console.log(JSON.stringify(_this.change_var))
		// console.log(`${_this.$options.__file}--------------初始化`)
		setTimeout(async () => {
			let { windowHeight } = await uni.getSystemInfoSync();
			_this.change_var.vh = windowHeight / 100;
			_this.change_var.win_height = _this.pxTorpx(windowHeight);
			_this.$forceUpdate();
			typeof _this.init_pagedata_callback === 'function' ? _this.init_pagedata_callback() : false;
		}, 100);
	},

	// 初始化状态
	// 1.用户是否登录  2.是否连接设备
	// page_login_judge 初始化完成
	com_page_show(_this) {
		getApp().globalData._this = _this;
		_this.update_pagedevice();
	},

	reset_carinfo() {
		uni.removeStorageSync('car_list');
		uni.removeStorageSync('car_info');
		uni.removeStorageSync('bike_cc_id');
		uni.removeStorageSync('bike_battery_mode');
		uni.removeStorageSync('bike_sound_setup');
		uni.removeStorageSync('bike_battery_id');
		uni.removeStorageSync('controller_mode_id');
	},
	clear_user(callback = 'refresh') {
		let _this = this;
		this.reset_carinfo();
		this.com_update_log_scan_warn(`清除用户信息：${callback}`);
		this.clear_bluetooth_cache();
		this.reset_bluetooth_cache({ ALL_CLEAR: true });
		uni.removeStorageSync('usertoken');
		uni.removeStorageSync('userid');
		uni.removeStorageSync('page_binding_list');
		uni.removeStorageSync('openid');
		uni.removeStorageSync('is_normal_user');
		_this.store_set_state_config({ state_name: 'USER_MSG', data: {} });
		_this.store_set_state({ state_name: 'USER_LOGIN', state_value: false });
		if (callback == 'refresh') {
			_this.PAGE_CALLBACK();
		}
	},
	PAGE_CALLBACK() {
		let _this = this;
		typeof _this.com_onshow_fun === 'function' ? _this.com_onshow_fun() : false;
	},

	COMMON_CALLBACK(_key) {
		let _this = this;
		console.log('COMMON_CALLBACK', _key);
		typeof _this[_key] === 'function' ? _this[_key]() : false;
	},

	com_getLocation() {
		let _this = this;
		return new Promise((resolve, reject) => {
			uni.getLocation({
				type: 'gcj02',
				success(res) {
					resolve({
						lat: res.latitude,
						lng: res.longitude,
					});
					// const latitude = res.latitude
					// const longitude = res.longitude
					// const speed = res.speed
					// const accuracy = res.accuracy
					// console.log(res)
				},
				fail(res) {
					console.log(res, 'getLocation-fail');
				},
			});
		});
	},

	// 调用地图导航
	myLocation(e) {
		let { latitude, longitude, name, address } = e;
		latitude = parseFloat(latitude);
		longitude = parseFloat(longitude);
		uni.openLocation({
			latitude,
			longitude,
			name,
			address,
			success(res) {
				console.log(res);
			},
			fail(res) {
				console.log(res);
			},
		});
	},

	com_chooseLocation() {
		let _this = this;
		uni.chooseLocation({
			success(res) {
				console.log(res);
				// console.log('位置名称：' + res.name);
				// console.log('详细地址：' + res.address);
				// console.log('纬度：' + res.latitude);
				// console.log('经度：' + res.longitude);
				_this.page_chooseLocation(res);
			},
		});
	},

	myToolsClick(e) {
		let { item, pagetype } = e;
		let _this = this;
		// console.log(e);
		if (pagetype == 'setting') {
			// 设置页
			if (!_this.judge_bluetooth()) return;
			let url = `/pagesA/index/setting_detail?type=${item.form_key}`;
			_this.goweburl_f(url);
		} else if (pagetype == 'user_device') {
			// 跳转设备保修保修页面
			_this.goweburl_f(`/pagesD/shop/user_warranty?dev_id=${item.id}`);
		} else {
			if (pagetype == 'control') {
				// 控制设备页面
				if (!_this.judge_bluetooth()) return;
			}

			if (item.url) {
				_this.goweburl_f(item.url);
				return;
			}

			let value = this.BLUETOOTH_INIT_FORM[item.form_key];
			if (!this.objJudge(value)) {
				value = '';
			}
			let btn = {
				confirm: '确认',
				cancel: '取消',
			};

			Object.assign(this.popup_config, item, btn);

			if (this.objJudge(item.content)) {
				this.popup_config['placeholder'] = '';
			} else if (this.objJudge(item.placeholder)) {
				this.popup_config['content'] = '';
			}
			this.$set(this.popup_config, 'inp_value', value);
			this.$nextTick(() => {
				this.openpopup();
			});
		}
	},

	async myCardFormClick(e) {
		let { item, form } = e;
		// console.log(item, 'mehtod.js----myCardFormClick')
		console.log(e, 'mehtod.js----myCardFormClick');
		let _this = this;
		if (item.state == 'switch') {
			// this.$refs.pagelogin.call_chang_model('edit_pwd');
			_this[form][item.key] = _this[form][item.key] == 1 ? 0 : 1;
			return;
		}

		if (item.url) {
			_this.goweburl_f(item.url);
		} else if (item.tel) {
			// _this.callphone(item.tel);
			_this.permission_to_check('callphone', () => {
				_this.callphone(item.tel);
			});
		} else if (item.key == 'cache') {
			// 清除缓存
			let cache = _this[form][item.key];
			if (cache) {
				_this.clear_cache(item);
			} else {
				_this.com_msg('暂无需要清除的缓存~');
			}
		} else if (item.key == 'logoff') {
			// 注销用户
			let f = await this.com_modal({
				title: '注销用户',
				content: ' 您正在进行账户注销操作。注销成功后，账户不可用，数据全部清除，此操作不可逆。',
			});
			if (!f) return;
			_this.logoff_user();
		} else if (item.key == 'password') {
			// 账号安全点击修改密码跳转当前页面pagelogin组件的修改密码功能
			// _this.$refs.pagelogin.call_chang_model('edit_pwd');
			_this.goweburl_f('/pagesA/index/login?type=edit_pwd');
		} else if (item.state == 'switch') {
			_this.$refs.pagelogin.call_chang_model('edit_pwd');
		} else if (item.popup_type) {
			_this.openpopup(item.popup_type);
		}
	},

	// 请求头处理
	header_handle(v) {
		let usertoken = uni.getStorageSync('usertoken');
		v['Authorization'] = usertoken;
		return v;
	},
};
